package com.sh.dc.controller;

import cn.hutool.core.bean.BeanUtil;
import com.sh.dc.common.BaseResult;
import com.sh.dc.common.pojo.bo.ProductSourceSaveBO;
import com.sh.dc.common.pojo.query.ProductSourcePageQuery;
import com.sh.dc.entity.ProductSource;
import com.sh.dc.service.IProductSourceService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Date;

/**
 * <p>
 * 制品源表 前端控制器
 * </p>
 *
 * @author zhenlan
 * @since 2024-07-10
 */
@RestController
@RequestMapping("/productSource")
public class ProductSourceController {

    @Autowired
    private IProductSourceService productSourceService;

    @PostMapping("/save")
    public BaseResult save(@Valid @RequestBody ProductSourceSaveBO saveBO) {
        ProductSource productSource = new ProductSource();
        BeanUtil.copyProperties(saveBO, productSource);
        productSource.setUpdateTime(new Date());
        return BaseResult.success(productSourceService.saveOrUpdate(productSource));
    }

    @GetMapping("/{id}")
    public BaseResult getById(@PathVariable("id") Long id) {
        return BaseResult.success(productSourceService.getById(id));
    }

    @PostMapping("/delete/{id}")
    public BaseResult delete(@PathVariable("id") Long id) {
        // TODO 需要判断是否有求解器在使用，制品源有求解器在使用时，删除失败
        return BaseResult.success(productSourceService.removeById(id));
    }

    @PostMapping("/page")
    public BaseResult page(@Valid @RequestBody ProductSourcePageQuery query) {
        return BaseResult.success(productSourceService.queryProductSourcePage(query));
    }

}
